import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

import static java.lang.Thread.MAX_PRIORITY;

public class ThreadPoolDemo3_线程工厂演示 {
    public static void main(String[] args) {
        // 1. 创建线程工厂
        ThreadFactory factory = new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                // 一定要把Runnable设置给新创建的线程
                Thread thread = new Thread(r);
                // 设置线程的命名规则
                thread.setName("->" + r.hashCode());
                // 设置线程优先级
                thread.setPriority(MAX_PRIORITY);
                return thread;
            }
        };
        ExecutorService service = Executors.newFixedThreadPool(5,factory);
        for (int i = 0; i < 5; i++) {
            service.submit(new Runnable() {
                @Override
                public void run() {
                    // 任务来了
                    System.out.println("我的" + Thread.currentThread().getName()+"号线程开始执行任务");
                }
            });
        }
    }
}
